home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Link list Vs Memory block
- Date: Fri, 16 Feb 96 17:33:05 GMT
- Organization: none
- Message-ID: <824491985snz@genesis.demon.co.uk>
- References: <4g0r86$fk5@netnews.upenn.edu>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4g0r86$fk5@netnews.upenn.edu>
- skramiah@gradine.cis.upenn.edu "Senthil K. Ramiah" writes:
-
- >Hi Everyone
- >Consider the following program segment.
- >
- >struct struct_s
- >{
- > int i;
- > char c;
- >};
- >main()
- >{
- > struct struct_s *s1;
- >
- > s1 = malloc(sizeof(struct struct_s) * 2);
-
- You should always test a call to malloc for failure.
-
- > s1->i =1; s1->c ='a';
- > s1++;
- > s1->i =2; s1->c = 'b';
- >}
- >
- >My questions are:
- >
- >1) How safe is it to use the above method
- > instead of a link list.
-
- There is no problem using malloc to allocate arrays (an array of structs
- in this case) - it is perfectly safe.
-
- >2) Will this implementation work on all platforms.
-
- Yes. However make sure you pass the correct pointer value when you
- call free. After executing s1++ above you would have to use free(s1-1).
- It is a good idea to keep a record of the exact pointer value returned by
- malloc.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-